package com.cutefool.zero.code.core.domain;

import com.cutefool.dsl.mybatis.core.objects.BasicsDomain;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Generated;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;

@Data
@EqualsAndHashCode(callSuper = true)
public class Db extends BasicsDomain<Db> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("TYPE_")
    private String type;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("HOST_")
    private String host;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("PORT_")
    private String port;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("USERNAME_")
    private String username;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("PASSWORD_")
    private String password;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("MULTIPLEX_")
    private Byte multiplex;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("NAME_")
    private String name;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("WRITABLE_")
    private Byte writable;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("MAX_CONN_")
    private Integer maxConn;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DBMS_ID_")
    private Long dbmsId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("ENV_")
    private String env;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("APP_ID")
    private Long appId;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("COMPANY_ID_")
    private Long companyId;

    private static final Db instance = new Db();

    private static final Table table = new Table();

    public static Db instance() {
        return instance;
    }

    @Override
    public Table table() {
        return table;
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<InsertDSL<Db>> insert() {
        return (InsertDSL<Db> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.type).toProperty("type");
            dsl.map(table.host).toProperty("host");
            dsl.map(table.port).toProperty("port");
            dsl.map(table.username).toProperty("username");
            dsl.map(table.password).toProperty("password");
            dsl.map(table.multiplex).toProperty("multiplex");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.writable).toProperty("writable");
            dsl.map(table.maxConn).toProperty("maxConn");
            dsl.map(table.dbmsId).toProperty("dbmsId");
            dsl.map(table.env).toProperty("env");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<MultiRowInsertDSL<Db>> insertMultiple() {
        return (MultiRowInsertDSL<Db> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.type).toProperty("type");
            dsl.map(table.host).toProperty("host");
            dsl.map(table.port).toProperty("port");
            dsl.map(table.username).toProperty("username");
            dsl.map(table.password).toProperty("password");
            dsl.map(table.multiplex).toProperty("multiplex");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.writable).toProperty("writable");
            dsl.map(table.maxConn).toProperty("maxConn");
            dsl.map(table.dbmsId).toProperty("dbmsId");
            dsl.map(table.env).toProperty("env");
            dsl.map(table.appId).toProperty("appId");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    public void updateAllColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.type).equalTo(this::getType)
           .set(table.host).equalTo(this::getHost)
           .set(table.port).equalTo(this::getPort)
           .set(table.username).equalTo(this::getUsername)
           .set(table.password).equalTo(this::getPassword)
           .set(table.multiplex).equalTo(this::getMultiplex)
           .set(table.name).equalTo(this::getName)
           .set(table.writable).equalTo(this::getWritable)
           .set(table.maxConn).equalTo(this::getMaxConn)
           .set(table.dbmsId).equalTo(this::getDbmsId)
           .set(table.env).equalTo(this::getEnv)
           .set(table.appId).equalTo(this::getAppId)
           .set(table.createBy).equalTo(this::getCreateBy)
           .set(table.createAt).equalTo(this::getCreateAt)
           .set(table.updateBy).equalTo(this::getUpdateBy)
           .set(table.updateAt).equalTo(this::getUpdateAt)
           .set(table.companyId).equalTo(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    public void updateSelectiveColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.type).equalToWhenPresent(this::getType)
           .set(table.host).equalToWhenPresent(this::getHost)
           .set(table.port).equalToWhenPresent(this::getPort)
           .set(table.username).equalToWhenPresent(this::getUsername)
           .set(table.password).equalToWhenPresent(this::getPassword)
           .set(table.multiplex).equalToWhenPresent(this::getMultiplex)
           .set(table.name).equalToWhenPresent(this::getName)
           .set(table.writable).equalToWhenPresent(this::getWritable)
           .set(table.maxConn).equalToWhenPresent(this::getMaxConn)
           .set(table.dbmsId).equalToWhenPresent(this::getDbmsId)
           .set(table.env).equalToWhenPresent(this::getEnv)
           .set(table.appId).equalToWhenPresent(this::getAppId)
           .set(table.createBy).equalToWhenPresent(this::getCreateBy)
           .set(table.createAt).equalToWhenPresent(this::getCreateAt)
           .set(table.updateBy).equalToWhenPresent(this::getUpdateBy)
           .set(table.updateAt).equalToWhenPresent(this::getUpdateAt)
           .set(table.companyId).equalToWhenPresent(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    @SuppressWarnings("all")
    public SqlColumn[] columns() {
        return new SqlColumn[]{table.id, table.type, table.host, table.port, table.username, table.password, table.multiplex, table.name, table.writable, table.maxConn, table.dbmsId, table.env, table.appId, table.createBy, table.createAt, table.updateBy, table.updateAt, table.companyId};
    }

    @Override
    public Map<String, SqlColumn<?>> columnsMap() {
        Map<String, SqlColumn<?>> maps = new LinkedHashMap<>(18);
        maps.put("id", table.id);
        maps.put("type", table.type);
        maps.put("host", table.host);
        maps.put("port", table.port);
        maps.put("username", table.username);
        maps.put("password", table.password);
        maps.put("multiplex", table.multiplex);
        maps.put("name", table.name);
        maps.put("writable", table.writable);
        maps.put("maxConn", table.maxConn);
        maps.put("dbmsId", table.dbmsId);
        maps.put("env", table.env);
        maps.put("appId", table.appId);
        maps.put("createBy", table.createBy);
        maps.put("createAt", table.createAt);
        maps.put("updateBy", table.updateBy);
        maps.put("updateAt", table.updateAt);
        maps.put("companyId", table.companyId);
        return maps;
    }

    @Override
    public SqlColumn<Long> id() {
        return table.id;
    }

    public static final class Table extends org.mybatis.dynamic.sql.AliasableSqlTable<Table> {
        public final SqlColumn<Long> id = column("ID_", JDBCType.BIGINT);

        public final SqlColumn<String> type = column("TYPE_", JDBCType.VARCHAR);

        public final SqlColumn<String> host = column("HOST_", JDBCType.VARCHAR);

        public final SqlColumn<String> port = column("PORT_", JDBCType.VARCHAR);

        public final SqlColumn<String> username = column("USERNAME_", JDBCType.VARCHAR);

        public final SqlColumn<String> password = column("PASSWORD_", JDBCType.VARCHAR);

        public final SqlColumn<Byte> multiplex = column("MULTIPLEX_", JDBCType.TINYINT);

        public final SqlColumn<String> name = column("NAME_", JDBCType.VARCHAR);

        public final SqlColumn<Byte> writable = column("WRITABLE_", JDBCType.TINYINT);

        public final SqlColumn<Integer> maxConn = column("MAX_CONN_", JDBCType.INTEGER);

        public final SqlColumn<Long> dbmsId = column("DBMS_ID_", JDBCType.BIGINT);

        public final SqlColumn<String> env = column("ENV_", JDBCType.VARCHAR);

        public final SqlColumn<Long> appId = column("APP_ID", JDBCType.BIGINT);

        public final SqlColumn<Long> createBy = column("CREATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> createAt = column("CREATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> updateBy = column("UPDATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> updateAt = column("UPDATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> companyId = column("COMPANY_ID_", JDBCType.BIGINT);

        public Table() {
            super("CUTE_DB", Table::new);
        }
    }
}